package com.gxa.autoparts.core.util;


import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.exception.BusinessException;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author xhy
 * @Date 2021/12/2 19:16
 */
public class DateUtil {
    public static String getWeekOfDate(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0){
            w = 0;
        }
        return weekDays[w];
    }

    public static Date formatDate(Date sysDate,int interval){
        Calendar cal=Calendar.getInstance();
        cal.setTime(sysDate);
        cal.add(Calendar.DATE, interval);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date time = cal.getTime();
        return time;
    }

    public static boolean isYeaterday(Date oldTime,Date newTime) throws BusinessException {
        if(newTime==null){
            newTime=new Date();
        }
        //将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = format.format(newTime);
        Date today = null;
        try {
            today = format.parse(todayStr);
        } catch (ParseException e) {
            throw new BusinessException(ExceptionCode.TIME_EXCCEPTION);
        }
        //昨天 86400000=24*60*60*1000 一天
        if((today.getTime()-oldTime.getTime())>0 && (today.getTime()-oldTime.getTime())<=86400000) {
            return true;
        }
        else if((today.getTime()-oldTime.getTime())<=0){ //至少是今天
            return false;
        }
        else{ //至少是前天
            return false;
        }

    }

//    public static int signIn(List<Score> scores) throws BusinessException {
//        int count=1;
//        for(int i=scores.size()-1;i>=0;i--){
//            try {
//                boolean yeaterday = DateUtil.isYeaterday(scores.get(i - 1).getChangetime(),
//                        scores.get(i).getChangetime());
//                if (yeaterday){
//                    count++;
//                }if (i-1==0){
//                    return count;
//                }
//            } catch (BusinessException e) {
//                throw new BusinessException(ExceptionCode.TIME_EXCCEPTION);
//            }
//        }
//        return 0;
//    }
    public static Date stringToDate(String date){
        Date date1 = new Date(date);
        return date1;
    }

    public static String getSecond(Date date2){
        Date date = new Date();
        long time = date.getTime();
        long time1 = date2.getTime();
        return String.valueOf(time1-time);
    }

    public static Date getAfterTenMinutes(Date date){
        Date date1 = new Date(date.getTime() + 60000);
        return date1;
    }

}
